// pages/addcarpage/addcarpage.js
var util = require('../../utils/util.js')
const app = getApp()
Page({
    data: {
        carInfo: { defaultCar: false },
        isDefault: false,
        isBack:true
    },
    onUnload() {
      
    },
    onLoad(param) {
        let carInfo = app.tempData.editCarData;
        console.log(param,carInfo);
        //从我的车辆列表修改按钮进入
        if (param && param.method == 'update') {
            let isDefault = carInfo.defaultCar;
            this.setData({
                carInfo: carInfo,
                isDefault: isDefault
            })
            app.tempData.editCarData={
                isEdit:true
                };
            //新增车辆
        } else if (param && param.method == 'add'){
            carInfo = {
                defaultCar: false
            };
            app.tempData.editCarData = {
                isEdit: false
            };
            //选择品牌后进入
        } else if (param && param.method == 'selectCarBrand'){
            
            var data = app.tempData.carBrandInfo;
            console.log(data);
            if (data) {
                carInfo.carBrand = data.id;
                carInfo.carInfo = data.carBrand;
                app.tempData.carBrandInfo = null;
            }
        }
        this.setData({
            carInfo: carInfo,
            isDefault: carInfo.defaultCar ? carInfo.defaultCar:false
        })
    },
    chooseCarBrand(){
        app.tempData.carBrandInfo = this.data.carInfo; 
        wx.navigateTo({
            url: '/pages/carbrand/carbrand',
        })
    },
    /**
     * 监听radioChange事件
     */
    radioChange: function(e) {
        this.data.carInfo.defaultCar = !this.data.carInfo.defaultCar;
        console.log(this.data.carInfo.defaultCar);
    },
    // 提交
    submit(e) {
        let that = this;
        let carInfo = this.data.carInfo;
        let defaultCar = this.data.carInfo.defaultCar ? '1' : '0';
        carInfo.defaultCar = defaultCar;

        let resMsg = this.checkPostData(carInfo)
        console.log(resMsg);
        if (resMsg != 'true') {
            wx.showModal({
                title: '警告',
                content: resMsg,
            })
            return 0;
        }
        let url = "/user/userCar/addUserCar";
        console.log(carInfo);
        let data ={
            userId: app.globalData.user.id,
            carBrand: carInfo.carBrand,
            carInfo: carInfo.carInfo,
            color: carInfo.color,
            licence: carInfo.licence,
            defaultCar: carInfo.defaultCar,
        }
        if (app.tempData.editCarData.isEdit){
            data.id = carInfo.id;
            url = "/user/userCar/updateUserCar";
        }
        console.log(data);
        // return;
        app.post(url, data, (res) => {
            if (res.code == 0) {
                app.tempData.editCarData = {
                    isEdit: false
                };
                wx.showToast({
                    title: '成功',
                    icon: 'success',
                    duration: 2000
                })
                app.tempData.myCarList = null;
                this.data.isBack = false;
                setTimeout(() => {
                    that.navigateToPage();
                }, 2000)
            } else {
                wx.showToast({
                    title: '保存失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    navigateToPage(){
        let navUrl = '/order/mycar/mycar';
        if(app.tempData.isChoose) {
            navUrl = '/pages/index/index';
            app.tempData.isChoose = false;
        }
        wx.redirectTo({
            url: navUrl
        })   
    },
    // 校验全部数据
    checkPostData(data) {
        console.log(data)
        // 车型
        if (!data.carBrand) {
            return "车型不能为空";
        }
        // 车牌号
        if (!data.licence) {
            return "车牌号不能为空";
        }
        // 车牌号
        if (util.checkCarBrand(data.licence)) {
            return "车牌号格式不正确";
        }
        // 颜色
        if (!data.color) {
            return "颜色不能为空";
        }
        return 'true';
    },

    // 获取车颜色
    getColor(e) {
        this.data.carInfo.color = e.detail.value;
        console.log(this.data.carInfo.color)
    },
    // 获取车牌数字
    getCheNum(e) {
        this.data.carInfo.licence = e.detail.value;
        console.log(this.data.carInfo.licence)
    }
})